package edu.gatech.oad.OTConfig;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import java.io.*;
import java.util.ArrayList;


public class FrameHolder {
	private JFrame current;
	private String[] data;
	public Intro intro;
	public ConfigFrameM5 party;
	public StoreFrameM6 store;
	public JButton load, continueTrail, trail, returnBack, btnStore;
	public JPanel buttons;
	public TrailFrame trailFrame;
	private ButtonListener listener;
	private WagonController wagonController;
	private StoreController controller;
	private ArrayList<Item> itemList;
	public FrameHolder(){
		
		intro = new Intro();
		load =  (JButton)intro.getLoadButton();
		load.setVisible(true);
		
		listener = new ButtonListener();
		load.addActionListener(listener);
		trail  = (JButton)intro.getTrailButton();
		trail.addActionListener(listener);
		trail.setVisible(true);
		
		intro.setVisible(true);
		//party.setVisible(false);
		
		
		// itemlist for the load methods
		itemList = new ArrayList<Item>();
		itemList.add(new Item("Oxen",0,0, 40));
		itemList.add(new Item("Food",0,0, 1));
		itemList.add(new Item("Clothing",0,0, 10));
		itemList.add(new Item("Ammunition Box x20",0,0, 2));
		itemList.add(new Item("Wheels",0,0, 10));
		itemList.add(new Item("Axle",0,0, 10));
		itemList.add(new Item("Tongue",0,0, 10));


		
		current = intro;

		
		

	}
	
	
	public static void main(String[] args){
		new FrameHolder();

	}

	public void loadThings(){
		File file = new File("data.txt");
		BufferedReader reader;
		try{
			reader = new BufferedReader(new FileReader(file));
			String line = reader.readLine();
			data = new String[26];
			int counter = 0;
			while(line != null){
				data[counter] = line;
				line = reader.readLine();
				counter++;
			}
			
		}
		catch(Exception e){
			
		}
		initializeThings();
	}
	public void initializeThings(){
		String ration = data[12];
		String selPace = data[11];
		Pace pace;
		if (selPace.equals("Steady"))
			pace=Pace.Steady;
		else if (selPace.equals("Grueling"))
			pace=Pace.Grueling;
		else
			pace=Pace.Strenuous;
		
		String[] players = new String[4];
		players[0] = data[0];
		players[1] = data[2];
		players[2] = data[4];
		players[3] = data[6];
		String playerName = data[8];
		String selProf = data[10];
		Profession profession;
		if (selProf.equals("Farmer"))
			profession=Profession.FARMER;
		else if (selProf.equals("Carpenter"))
			profession=Profession.CARPENTER;
		else
			profession=Profession.BANKER;
		
		wagonController = new WagonController(ration,players,pace,playerName, profession);
		//set settler1  health
		if(data[1].equals("HEALTHY")){
			Wagon.setSettlerHealth(Wagon.getSettlerOne(), "healthy");
		}
		else if(data[1].equals("ILL")){
			Wagon.setSettlerHealth(Wagon.getSettlerOne(), "ill");
		}
		else{
			Wagon.setSettlerHealth(Wagon.getSettlerOne(), "dead");
		}
		
		//set settler1  health
		if(data[3].equals("HEALTHY")){
			Wagon.setSettlerHealth(Wagon.getSettlerTwo(), "healthy");
		}
		else if(data[3].equals("ILL")){
			Wagon.setSettlerHealth(Wagon.getSettlerTwo(), "ill");
		}
		else{
			Wagon.setSettlerHealth(Wagon.getSettlerTwo(), "dead");
		}
		if(data[5].equals("HEALTHY")){
			Wagon.setSettlerHealth(Wagon.getSettlerThree(), "healthy");
		}
		else if(data[5].equals("ILL")){
			Wagon.setSettlerHealth(Wagon.getSettlerThree(), "ill");
		}
		else{
			Wagon.setSettlerHealth(Wagon.getSettlerThree(), "dead");
		}
		if(data[7].equals("HEALTHY")){
			Wagon.setSettlerHealth(Wagon.getSettlerFour(), "healthy");
		}
		else if(data[7].equals("ILL")){
			Wagon.setSettlerHealth(Wagon.getSettlerFour(), "ill");
		}
		else{
			Wagon.setSettlerHealth(Wagon.getSettlerFour(), "dead");
		}
		buyThings();
	}
	
	public void buyThings(){
		store = new StoreFrameM6(wagonController);
		controller = store.getController();
		int total = 0;
		for(int temp = 0;temp<7;temp++){
			Item toBuy = itemList.get(temp);
			int temp2;
			
			controller.buyItem(toBuy, Integer.parseInt(data[13+temp]));
			temp2 = Integer.parseInt(data[13+temp]) * controller.getStoreInventory().get(temp).getPrice();
			total = total + temp2;
		}
		
		controller.getWagonInventory().setMoney(controller.getWagonInventory().getMoney()- total);
		store.setVisible(false);
		returnBack = store.getReturn();
		returnBack.addActionListener(listener);
		trailFrame = new TrailFrame(wagonController);
		trailFrame.getTrailPanel().setPaceFocus(data[11]);
		trailFrame.getTrailPanel().setRationFocus(data[12]);
		
		int daysOnTrail = Integer.parseInt(data[21]);
		int milesTravelled = Integer.parseInt(data[22]);
		int milesToNext = Integer.parseInt(data[23]);
		int currentIndex = Integer.parseInt(data[24]);
		String location = data[20];
		
		trailFrame.getTrailPanel().getJourneyController().getJourney().setDaysOnJourney(daysOnTrail);
		trailFrame.getTrailPanel().getJourneyController().getJourney().setMilesTraveled(milesTravelled);
		trailFrame.getTrailPanel().getJourneyController().getJourney().setLocation(location);
		trailFrame.getTrailPanel().getJourneyController().getTrailNavigator().goToLocation(currentIndex);
		trailFrame.getTrailPanel().getJourneyController().getTrailNavigator().setMilesToNext(milesToNext);
		trailFrame.getTrailPanel().updateInventoryText();
		trailFrame.getTrailPanel().updateLabels();
		btnStore = (JButton) trailFrame.getBtnStore();
		btnStore.addActionListener(listener);
		trailFrame.setVisible(true);
	}

	
	private class ButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(e.getSource() == load){
				current.setVisible(false);
				loadThings();
			}
			
			if(e.getSource()== trail){
				party = new ConfigFrameM5(wagonController); // Create new party frame
				continueTrail = (JButton)party.getContinue();
				continueTrail.addActionListener(listener);
				party.setVisible(true);
				current.setVisible(false);
				current = party;
			}
			if(e.getSource()== continueTrail){
				wagonController = party.getWagonControlller();
				if(store == null){
					store = new StoreFrameM6(wagonController);
				}
				store.setVisible(true);
				current.setVisible(false);
				returnBack = store.getReturn();
				returnBack.addActionListener(listener);
				current = store;
			}
			if(e.getSource()== returnBack){
				if(trailFrame == null){
					trailFrame = new TrailFrame(wagonController);
				}
				btnStore = (JButton) trailFrame.getBtnStore();
				btnStore.addActionListener(listener);
				current.setVisible(false);
				trailFrame.getTrailPanel().updateLabels();
				trailFrame.getTrailPanel().updateInventoryText();
				trailFrame.setVisible(true);
				
				current = trailFrame;
			}
			if(e.getSource() == btnStore){
				current.setVisible(false);
				store.setVisible(true);
			//	store.getStorePanel().btnBuy.doClick(10);
				current = store;
				if(!trailFrame.getTrailPanel().getStoreVisited()){
					store.getStoreController().repopulateStore();
				}
				store.getStoreController().reprice(trailFrame.getTrailPanel().getMultiplier());
				store.getStorePanel().updatePriceLabels();
				store.getStorePanel().updateLabels();
			}
		}
	}
}
